//
//  NIDropDown.m
//  NIDropDown
//
//  Created by Bijesh N on 12/28/12.
//  Copyright (c) 2012 Nitor Infotech. All rights reserved.
//

#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"
#import "Util.h"

@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UIButton *btnSender;
@property(nonatomic, retain) NSArray *list;
@property(nonatomic, retain) NSArray *imageList;
@end

@implementation NIDropDown
@synthesize table;
@synthesize btnSender;
@synthesize list;
@synthesize imageList;
@synthesize delegate;
@synthesize animationDirection;

- (id)showDropDown:(UIButton *)b withHeight:(CGFloat *)height withContent:(NSArray *)arr withImg:(NSArray *)imgArr withDirect:(NSString *)direction withWidth:(CGFloat *)width withX:(CGFloat *)xx withY:(CGFloat *)yy withView:(UIView *)view {
    self.backgroundColor=[UIColor blackColor];
    btnSender = b;
    animationDirection = direction;
    self.table = (UITableView *)[super init];
    if (self) {
        // Initialization code
        //CGRect btn = b.frame;
        self.list = [NSArray arrayWithArray:arr];
        self.imageList = [NSArray arrayWithArray:imgArr];
//        if ([direction isEqualToString:@"up"]) {
//            self.frame = CGRectMake(*xx, *yy, *width, 0);
//            self.layer.shadowOffset = CGSizeMake(-5, -5);
//        }else if ([direction isEqualToString:@"down"]) {
//            self.frame = CGRectMake(*xx, *yy, *width, 0);
//            self.layer.shadowOffset = CGSizeMake(-5, 5);
//        }
        
//        self.layer.masksToBounds = NO;
//        self.layer.cornerRadius = 8;
//        self.layer.shadowRadius = 5;
//        self.layer.shadowOpacity = 0.5;
        
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, *width, 0)];
        table.delegate = self;
        table.dataSource = self;
        table.layer.borderColor=[UIColor colorWithRed:0/255.0 green:111/255.0 blue:107/255.0 alpha:1.0].CGColor;
        table.layer.borderWidth=1.0f;
//        table.layer.cornerRadius = 5;
        table.showsVerticalScrollIndicator=NO;
        table.backgroundColor = [UIColor lightGrayColor]; //[UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
//        table.separatorStyle = UITableViewCellSeparatorStyleNone;
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        table.separatorColor = [UIColor grayColor];
        
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:0.1];
        //if ([direction isEqualToString:@"up"]) {
            //self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
        //} else if([direction isEqualToString:@"down"]) {
            self.frame = CGRectMake(*xx, *yy, *width, *height);
        //}
        table.frame = CGRectMake(0, 0, *width, *height);
        //[UIView commitAnimations];
        [view addSubview:self];
        //[b.superview addSubview:self];
//        [b.superview bringSubviewToFront:self];
        [self addSubview:table];
        [self setExtraCellLineHidden:table];
    }
    return self;
}
- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}
-(void)hideDropDown:(UIButton *)b {
//    CGRect btn = b.frame;
//    
    //[UIView beginAnimations:nil context:nil];
    //[UIView setAnimationDuration:0.1];
//    if ([animationDirection isEqualToString:@"up"]) {
//        self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
//    }else if ([animationDirection isEqualToString:@"down"]) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.width, 0);
//    }
    table.frame = CGRectMake(0, 0, table.width, 0);
    //[UIView commitAnimations];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //8.0的分割线额外设置
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        
        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
            [cell setPreservesSuperviewLayoutMargins:NO];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 33;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    }
    cell.textLabel.text =[list objectAtIndex:indexPath.row];
    
    
//    if ([self.imageList count] == [self.list count]) {
//        cell.textLabel.text =[list objectAtIndex:indexPath.row];
//        cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//    } else if ([self.imageList count] > [self.list count]) {
//        cell.textLabel.text =[list objectAtIndex:indexPath.row];
//        if (indexPath.row < [imageList count]) {
//            cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//        }
//    } else if ([self.imageList count] < [self.list count]) {
//        cell.textLabel.text =[list objectAtIndex:indexPath.row];
//        if (indexPath.row < [imageList count]) {
//            cell.imageView.image = [imageList objectAtIndex:indexPath.row];
//        }
//    }
    
    cell.textLabel.textColor = [UIColor blackColor];
    
//    UIView * v = [[UIView alloc] init];
//    v.backgroundColor = [UIColor grayColor];
//    cell.selectedBackgroundView = v;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self hideDropDown:btnSender];
    
    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
    //[btnSender setTitle:c.textLabel.text forState:UIControlStateNormal];
    
//    for (UIView *subview in btnSender.subviews) {
//        if ([subview isKindOfClass:[UIImageView class]]) {
//            [subview removeFromSuperview];
//        }
//    }
//    imgView.image = c.imageView.image;
//    imgView = [[UIImageView alloc] initWithImage:c.imageView.image];
//    imgView.frame = CGRectMake(5, 5, 25, 25);
//    [btnSender addSubview:imgView];
    [self myDelegate];
    [self.delegate feedBackInfo:self withRow:(int)indexPath.row withCotent:c.textLabel.text];
}

- (void) myDelegate {
    [self.delegate niDropDownDelegateMethod:self];
}

-(void)dealloc {
//    [super dealloc];
//    [table release];
//    [self release];
}

@end
